package com.icss.train_sys_back.controller.admin.train_manage;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.icss.train_sys_back.entity.ResponseResult;
import com.icss.train_sys_back.entity.ResultEnum;
import com.icss.train_sys_back.entity.train_manage.TrainEntity;
import com.icss.train_sys_back.service.train_manage.TrainService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author 小泽
 * @createTime 2025/9/25 9:18
 */
@RestController
@RequestMapping("/admin/train_manage")
@Slf4j
public class TrainController {

    @Autowired
    private TrainService trainService;

    //获取车辆列表
    //分页+条件搜索（如果有）
    @GetMapping("/getList")
    public ResponseResult getList(@RequestParam(defaultValue = "1") Integer page,
                                  @RequestParam(defaultValue = "10") Integer pageSize,
                                  @RequestParam(required = false) String trainNumber,
                                  @RequestParam(required = false) String trainType,
                                  @RequestParam(required = false) Integer status) {

        log.info("车辆列表分页+条件查询，page = {},pageSize = {} , " +
                        "trainNum = {} , trainType = {} , status = {}",
                page, pageSize, trainNumber, trainType, status);
        PageHelper.startPage(page, pageSize);
        List<TrainEntity> trainEntityList = trainService.findTrainListByConditions(trainNumber, trainType, status);
        log.info("结果：{}", trainEntityList);
        PageInfo<TrainEntity> pageInfo = new PageInfo<>(trainEntityList);

        return ResponseResult.success(pageInfo);

    }

    //添加车辆
    @PostMapping("/save")
    public ResponseResult save(@RequestBody TrainEntity train) {
        Integer trainId = train.getTrainId();
        if (trainId == null) {
            //新增
            log.info("新增方法：入参 train = {}", train);
            boolean res = trainService.insert(train);
            return res ? ResponseResult.success() : ResponseResult.fail(ResultEnum.FAIL);
        }else {
            //更新
            log.info("更新方法：入参 train = {}", train);
            boolean res = trainService.update(train);
            return res ? ResponseResult.success() : ResponseResult.fail(ResultEnum.FAIL);
        }
    }

    //根据id删除车辆
    @DeleteMapping("/delete/{id}")
    public ResponseResult<Object> delete(@PathVariable("id") Integer id) {
        log.info("根据 id = {} 删除车辆", id);
        boolean res = trainService.delete(id);
        return res ? ResponseResult.success() : ResponseResult.fail(ResultEnum.FAIL);
    }



}
